package com.smmisia.article.controller;

import com.smmisia.article.dto.address.AddressDTO;
import com.smmisia.article.dto.address.UpdateAddressDTO;
import com.smmisia.article.entity.Address;
import com.smmisia.article.service.AddressService;
import com.smmisia.common.Result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/address")
public class AddressController {

    //TODO 下单前检查是否有地址

    @Autowired
    private AddressService addressService;

    /**
     * 获取一级省市
     *
     * @return
     */
    @GetMapping("/getProvince")
    public Result<Map<Integer, String>> getProvince() {
        return addressService.getProvince();
    }

    /**
     * 获取二级省市
     *
     * @return
     */
    @GetMapping("/getCity")
    public Result<Map<Integer, String>> getCity(@RequestParam(name = "provinceId") Integer provinceId) {
        return addressService.getCity(provinceId);
    }


    /**
     * 获取三级省市
     *
     * @return
     */
    @GetMapping("/getCountry")
    public Result<Map<Integer, String>> getCounty(@RequestParam(name = "provinceId") Integer provinceId) {

        return addressService.getCounty(provinceId);

    }

    /**
     * 添加地址
     *
     * @param addressDTO
     * @return
     */
    @PostMapping("/add")
    public Result<Address> add(@RequestBody AddressDTO addressDTO) {
        return addressService.add(addressDTO);
    }


    /**
     * 删除地址
     * @param id
     * @return
     */
    @GetMapping("/delete")
    public Result<String> delete(@RequestParam(name = "id") Integer id) {
        return addressService.delete(id);
    }

    /**
     * 修改地址
     *
     * @param updateAddressDTO
     * @return
     */
    @PostMapping("/update")
    public Result<Address> update(@RequestBody UpdateAddressDTO updateAddressDTO) {
        return addressService.updateAddress(updateAddressDTO);
    }


    @GetMapping("/get")
    public Result<List<Address>> get() {
        return addressService.getInfo();
    }
}